package com.backend.werewolfkillingbackend.contorller;

import com.backend.werewolfkillingbackend.common.R;
import com.backend.werewolfkillingbackend.pojo.Player;
import com.backend.werewolfkillingbackend.pojo.User;
import com.backend.werewolfkillingbackend.service.PlayerService;
import com.backend.werewolfkillingbackend.service.UserService;
import com.backend.werewolfkillingbackend.util.JsonUtil;
import com.backend.werewolfkillingbackend.util.NumberGenerator;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/private/v1")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private PlayerService playerService;

    @GetMapping("/login")
    public R<Object> login(@RequestParam(name = "username") String username, @RequestParam(name = "password") String password) throws JsonProcessingException {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, username);
        User one = userService.getOne(queryWrapper);

        if (one == null) {
            return R.error("用户不存在");
        }
        if (!one.getPassword().equals(password)) {
            return R.error("用户名或密码错误");
        }

        userService.updateById(one);

        LambdaQueryWrapper<Player> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Player::getUId, one.getId());
        Player player = playerService.getOne(wrapper);
        if (player == null) {
            player = new Player();
            player.setName(username);
            player.setUId(Long.valueOf(one.getId()));
            player.setId(Long.valueOf(NumberGenerator.generateNumber()));
            player.setAvatar(one.getAvatar());
            one.setPlayer(JsonUtil.parseObjToJson(player));
            userService.updateById(one);
            playerService.save(player);
        }

        return R.success(one, "登录成功");
    }

    @PostMapping("/user")
    public R<Object> addUser(@RequestBody Map<String, String> userInfo) {
        String username = userInfo.get("username");
        String password = userInfo.get("password");
        String email = userInfo.get("email");
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(User::getUsername, username);
        User one = userService.getOne(queryWrapper);
        if (one != null) {
            return R.error("用户已经存在");
        }

        User user = new User();
        user.setEmail(email);
        user.setPassword(password);
        user.setUsername(username);
        userService.save(user);
        return R.success(user, "注册成功");
    }

    @GetMapping("/info/{id}")
    public R<User> getUserInfoById(@PathVariable String id) {
        User user = userService.getById(id);
        return R.success(user, "获取成功");
    }

}
